<?php
header("Content-Type: text/html; charset=utf-8");

$tweet_from_twitter = false;

$id = $_REQUEST['id'];

$jsonurl = "http://nibble.li/twittertido/web/getTweet.php?id=$id";
$json = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json, true);
$arrays = $json_output['tweets'];

$tweet_nibble = $arrays[0];

$jsonurl = "http://nibble.li/twittertido/web/getLike.php?tweet_id=$id";
$like = file_get_contents($jsonurl, 0, null, null);

$date = $tweet_nibble['date_created'];
$screen_name = $tweet_nibble['user_tweet'];
$tweet_text = $tweet_nibble['tweet'];

$tweet_text_twitter = $tweet_nibble['tweet'];
if (strlen($tweet_text_twitter) > 102) {
    $tweet_text_twitter = substr($tweet_text_twitter, 0, 100) . "... ";
}

$jsonurl = "http://nibble.li/twittertido/web/getComments.php?tweet_id=$id";
$json = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json, true);
$comments = $json_output['comments'];
?>
<?php include_once("detect.php"); ?>
<!-DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title><?php print $tweet_text ?> (<?php print $screen_name ?>)</title>
        <meta property="og:title" content="<?php print $tweet_text; ?> v&iacute;a Twittertido" />
        <meta property="og:description" content="Twittertido! Los mejores y m&aacute;s graciosos tweets en espa&ntilde;ol!" />
        <meta property="og:image" content="http://twittertido.com.es/img/logo_128.png" />
        <link rel="stylesheet" href="css/glamour.css" />
        <script src="js/jquery-1.6.2.min.js"></script>
        <script src="js/common.js"></script>
        <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
        <script type="text/javascript">
            window.___gcfg = {lang: 'es'};

            (function() {
                var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                po.src = 'https://apis.google.com/js/plusone.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
            })();
        </script>
    </head>
    <body>

        <?php include_once("header.php"); ?>

        <div class="wrapper">
            <?php
            $bg = rand(1, 9);
            $class = "bg_$bg";
            ?>
            <div class="content">
                <div style="display: block" class="mlfive mt_five">
                    <div class="fl mlfive mt_3" style="width: 50px">
                        <g:plusone size="small" href="http://twittertido.com.es/tweet.php?id=<?php print $id; ?>"></g:plusone>
                    </div>
                    <div class="fl mlfive">
                        
                    </div>
                    <div class="fl mlfive">
                        <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://twittertido.com.es/tweet.php?id=<?php print $id; ?>" data-text="<?php print "$tweet_text_twitter http://twittertido.com.es/tweet.php?id=$id"; ?>" data-via="tuittertido" data-lang="es"></a>
                    </div>
                    <div class="clear"></div>
                </div>

                <div class="wrapper_tweet <?php print $class; ?>">
                    <div class="tweet">
                        <table>
                            <?php if (!is_null($date) && $date != "null") { ?>
                                <tr><td colspan="2"><div class="fr date">
                                            <?php print date('d/m/Y H:i:s', strtotime($date)); ?>
                                        </div></td></tr>
                            <?php } ?>
                            <tr>
                                <td class="avatar">
                                    <img src="https://api.twitter.com/1/users/profile_image?screen_name=<?php print $screen_name; ?>&size=normal" />
                                </td>
                                <td style="padding-left: 10px;">
                                    <div class="title"><?php print $screen_name; ?></div>
                                    <div><?php print $tweet_text; ?></div>
                                </td>
                            </tr>
                        </table>
                        <table class="tweet_actions">
                            <tr>
                                <td>
                                    <div class="fl mlfive pointer" onclick="like(<?php print $tweet['id']; ?>)">
                                        <img id="ic_like_<?php print $tweet_nibble['id']; ?>" src="img/ic_like.png" />
                                    </div>
                                    <div class="fl mlfive pointer" onclick="dislike(<?php print $tweet['id']; ?>)">
                                        <img id="ic_dislike_<?php print $tweet_nibble['id']; ?>" src="img/ic_dislike.png" />
                                    </div>
                                </td>
                                <td>
                                    <?php if ($tweet_from_twitter) { ?>
                                        <a href="http://twitter.com/intent/tweet?in_reply_to=<?php print $tweet_nibble['tweet_url']; ?>" target="_blank">                                
                                            <img src="img/ic_action_reply_default.png" />
                                        </a>
                                    <?php } else { ?>
                                        &nbsp;
                                    <?php } ?>
                                </td>
                                <td>
                                    <?php if ($tweet_from_twitter) { ?>
                                        <a href="http://twitter.com/intent/retweet?tweet_id=<?php print $tweet_nibble['tweet_url']; ?>" target="_blank">
                                            <img src="img/ic_action_rt_off_default.png" />
                                        </a>
                                    <?php } else { ?>
                                        &nbsp;
                                    <?php } ?>
                                </td>
                                <td>
                                    <div class="fr mrfive pointer">
                                        <a href="tweet.php?id=<?php print $tweet_nibble['id']; ?>"><img src="img/ic_action_comments.png" /></a>
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <br />
            <script type="text/javascript"><!--
                google_ad_client = "ca-pub-4013617504060877";
                /* Twittertido Desktop */
                google_ad_slot = "7224782497";
                google_ad_width = 468;
                google_ad_height = 60;
                //-->
            </script>
            <script type="text/javascript"
                    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script>
            <br />
            <div class="content">
                <div class="title mlfive">Comentarios</div>
                <div class="comments">
                    <div class="clear"></div>
                    <table style="width: 100%;">
                        <?php foreach ($comments as $comment) { ?>
                            <tr class="">
                                <td>
                                    <div class="fl metadata"><?php print $comment['username']; ?></div>
                                    <div class="fr mrfive metadata date"><?php print date('d/m/Y H:i:s', strtotime($comment['date'])); ?></div>
                                    <div class="clear"></div>
                                    <div style="clear: both"><?php print $comment['comment']; ?></div>
                                </td>
                            </tr>
                            <tr><td><hr /></td></tr>
                        <?php } ?>
                        <tr><td colspan="2" class="title small">Tu comentario</td></tr>
                        <tr>
                            <td>
                                <?php if (isLogged()) { ?>
                                    <form>
                                        <div class="error dn" id="error_comment">Ha ocurrido un error al enviar el comentario</div>
                                        <input type="hidden" id="tweet_id" value="<?php print $tweet_nibble['id']; ?>" />
                                        <input type="text" id="user" name="user" placeholder="Nombre de usuario" />
                                        <textarea type="text" id="comment" name="comment" placeholder="Tu comentario" ></textarea>
                                        <div class="button fr" id="button_comment" onclick="comment()" >Enviar</div>
                                    </form>
                                <?php } else { ?>
                                    <br /><div class="error">Para poder comentar es necesario que te <a href="">registres</a> o inicies sesi&oacute;n</div><br />
                                    <br />
                                <?php } ?>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>